package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.BusinessMapper;
import com.fs.mapper.InventoryMapper;
import com.fs.mapper.ReturnToMapper;
import com.fs.pojo.Business;
import com.fs.pojo.Order;
import com.fs.pojo.OrderDetails;
import com.fs.pojo.ReturnToVendor;
import com.fs.service.BusinessService;
import com.fs.service.OrderService;
import com.fs.service.ReturnToService;
import com.fs.vo.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author hyy
 * @version 1.0
 * @description: 销售退货实现
 * @date 2024/10/22 17:25
 */
@Service
public class ReturnToServiceImpl implements ReturnToService {
    @Autowired
    private ReturnToMapper returnToMapper;
    @Resource
    private OrderService orderService;
    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private BusinessMapper businessMapper;
    @Override
    public Page<ReturnToVendor> querylist(Condition<ReturnToVendor> condition) {
        Page<ReturnToVendor> page = new Page<>(condition.getPage(),condition.getCount());
        //构建条件构造器
        LambdaQueryWrapper<ReturnToVendor> wrapper = new LambdaQueryWrapper<ReturnToVendor>();
        ReturnToVendor returnToVendor=condition.getSearch();
        if (Objects.nonNull(returnToVendor)){
            //供应商姓名模糊查询条件
            wrapper.like(returnToVendor.getRtvId()!=null,ReturnToVendor::getRtvId,returnToVendor.getRtvId())
                    //编号查询
                    .eq(returnToVendor.getOrderId()!=null,ReturnToVendor::getOrderId,returnToVendor.getOrderId())

                    .eq(returnToVendor.getRtvStatus()!=null,ReturnToVendor::getRtvStatus,returnToVendor.getRtvStatus());
        }
        return returnToMapper.selectPage(page,wrapper);
    }

    @Override
    public ReturnToVendor getOrderById(Integer id) {
        ReturnToVendor returnToVendor = returnToMapper.selectById(id);
        Order order = orderService.getOrderById(returnToVendor.getOrderId());
        returnToVendor.setOrder(order);
        return returnToVendor;
    }

    /**
     * 生成退单信息
     * @param returnToVendor
     * @return
     */
    @Transactional
    @Override
    public Integer addReturnToVendor(ReturnToVendor returnToVendor) {
        //获取对应的订单信息
        Order order = orderService.getOrderById(returnToVendor.getOrderId());
        //修改库存信息————添加
        for (OrderDetails orderDetail : order.getOrderDetails()) {
            inventoryMapper.updateNumById(orderDetail.getProductId(),(-1)*orderDetail.getTotal());
        }
        //添加的营业额信息删除
        businessMapper.delete(new LambdaQueryWrapper<Business>().eq(Business::getStoProId,returnToVendor.getOrderId())
                .eq(Business::getStoType,0));

        //修改订单状态，时间
        order.setOrderStatus(2);
        order.setOrderDate(new Date());
        orderService.updateOrder(order);
        //生成退货订单
        returnToVendor.setRtvTime(new Date());
        return returnToMapper.insert(returnToVendor);
    }

    @Override
    public Boolean delReturnToVendor(Integer id) {
        return returnToMapper.deleteById(id)!=0?true:false;

    }

    /**
     * 修改退货信息，状态修改为0时，不允许退货
     * @param returnToVendor
     * @return
     */
    @Transactional
    @Override
    public Integer updatReturnToVendor(ReturnToVendor returnToVendor) {
        //允许退货
        if (returnToVendor.getRtvStatus() == 0) {
            //获取对应的订单信息
            Order order = orderService.getOrderById(returnToVendor.getOrderId());
            //修改库存信息————添加
            for (OrderDetails orderDetail : order.getOrderDetails()) {
                inventoryMapper.updateNumById(orderDetail.getProductId(),(-1)*orderDetail.getTotal());
            }
            //添加的营业额信息删除
            businessMapper.delete(new LambdaQueryWrapper<Business>().eq(Business::getStoProId,returnToVendor.getOrderId()));
            //修改订单状态，时间
            order.setOrderStatus(0);
            order.setOrderDate(new Date());
            orderService.updateOrder(order);
        }
        return returnToMapper.updateById(returnToVendor);
    }
}
